package com.zou.study.zmy;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.net.*;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <br/>Date 2021/8/20
 * <br/>Time 13:51:10
 *
 * @author _blank
 */
public class FetchMarkDownUrl {

    public static final String REGEX = ".*!\\[[\\w.]+]\\(([\\w/:.-]+)\\).*";
    public static final Pattern PATTERN = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);

    public static void main(String[] args) throws IOException, URISyntaxException {
        final StopWatch stopWatch = new StopWatch("blank");
        stopWatch.start();

        final File file = new File("C:\\Users\\_blank\\Desktop\\com\\QQ群.txt");
        final Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        try (final FileReader in = new FileReader(file); final BufferedReader br = new BufferedReader(in)) {
            String readLine;
            while (Objects.nonNull(readLine = br.readLine())) {

                final Matcher matcher = PATTERN.matcher(readLine);
                while (matcher.matches()) {
                    final String group = matcher.group(1);

                    final URI uri = new URI(group);
                    final URL url = uri.toURL();
                    final URLConnection urlConnection = url.openConnection();
                    if (urlConnection instanceof HttpURLConnection) {
                        urlConnection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(60L));
                        urlConnection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(60L));
                        urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
                        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36");

                        try (final InputStream urlIn = urlConnection.getInputStream()) {
                            final String idStr = snowflake.nextIdStr();

                            final File newFile = new File("C:\\Users\\_blank\\Desktop\\com\\QQ群", idStr);
                            FileUtils.copyToFile(urlIn, newFile);

                            String append = idStr;
                            try (FileInputStream fis = new FileInputStream(newFile)) {
                                final byte[] bytes = new byte[28];
                                final int read = fis.read(bytes, 0, 28);
                                if (read != -1) {
                                    final StringBuilder sb = new StringBuilder(bytes.length * 3);
                                    for (byte aByte : bytes) {
                                        final int i1 = aByte & 0xFF;
                                        final String toHexString = Integer.toHexString(i1);
                                        if (toHexString.length() == 1) {
                                            sb.append("0");
                                        }
                                        sb.append(toHexString);
                                    }
                                    final String lowerCase = sb.toString().toLowerCase();
                                    if (lowerCase.startsWith("89504e47")) {
                                        append += ".png";
                                    } else if (lowerCase.startsWith("ffd8ff")) {
                                        append += ".jpg";
                                    }
                                }
                            }

                            final boolean b = newFile.renameTo(new File(newFile.getParentFile(), append));
                            readLine = readLine.replace(group, ".\\zjx\\" + append);
                        }
                    }
                }

                System.out.println(readLine);
            }
        }

        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }

}
